package com.itheima.health.controller;

import com.itheima.interfaces.CheckGroupService;
import com.itheima.pojo.CheckGroup;
import com.itheima.resources.MessageConstant;
import com.itheima.resources.entity.QueryPageBean;
import com.itheima.resources.entity.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //有条件和无条件都是一个方法 分页查询
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            Map map = checkGroupService.findPageByContion(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //逻辑删除
    @RequestMapping("deleteCheckItemById.do")
    public Result deleteCheckItemById(@RequestParam("id") Integer id) {
        try {
            //  update  xxx  set is_delete = 1 where id = 1
            CheckGroup checkGroup = new CheckGroup();
            checkGroup.setId(id);
            checkGroup.setIs_delete(1); //  修改字段值
            checkGroupService.updateById(checkGroup);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
        }

    }

    //添加
    @RequestMapping("add.do")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam("checkitemIds") Integer[] checkitemIds) {
        //     有无 ID     如果实体类存在 id   update 操作 否则 插入操作
        try {
            checkGroupService.add(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    //  有条件和无条件查询 都是同一个方法
    @RequestMapping("findAll.do")
    public Result findAll() {
        try {
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
    //检查项多选框查询
//    @RequestMapping("findAll")
//    public Result findAll(){
//        try {
//            QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("is_delete",0);
//            List<CheckGroup> list = checkGroupService.list(queryWrapper);
//            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(true,MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//    }
}
